package com.wms.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.wms.service.UserService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class CodeGenrator {

    @Resource
    private UserService userService;

    @GetMapping("/user/list")
    public List<User> list()
    {
        //return  userService.list();
        return userService.listAll();
    }

    //新增
    @PostMapping("/user/save")
    public boolean save(@RequestBody User user){
        return userService.save(user);
    }
    //修改
    @PostMapping("/user/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }
    //新增或修改
    @PostMapping("/user/saveOrMod")
    public boolean saveOrMod(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    //删除
    @GetMapping("/user/delete")
    public boolean delete( Integer id){
        return userService.removeById(id);
    }
    //查询(模糊，匹配)

    @PostMapping("/user/listPage")
    public List listPage(@RequestBody User user){
//        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(User::getName,user.getName());
//        return userService.list(queryWrapper);

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(user.getName())){
            wrapper.like(User::getName, user.getName());
        }
        return  userService.list(wrapper);


    }
    @PostMapping("/user/listPageC1")
    public Result listPageC1(@RequestBody QueryPageParam Query){

        HashMap param = Query.getParam();
        String name = (String)param.get("name");
        System.out.println("name==="+(String)param.get("name"));

        Page<User> page = new Page();
        page.setCurrent(Query.getPageNum());
        page.setSize(Query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(User::getName,name);

        //IPage result =  userService.pageC(page);
        IPage result =  userService.pageCC(page,lambdaQueryWrapper);

        System.out.println("total=="+result.getTotal());
        if (result.getTotal()==0){
            return  Result.fail();
        }else {
            return Result.suc(result.getRecords(),result.getTotal());
        }




    }
}
